#include "stdafx.h"

#include <memory>

#include "tinyxml.h"

#include "Config.h"

namespace PC2
{

void Config::GetApplicationSettings( std::string const& aPath, ApplicationSettings* apSettings )
{
	std::auto_ptr<TiXmlDocument> lpXmlDoc;
	lpXmlDoc.reset(new TiXmlDocument());

	bool lOK = lpXmlDoc->LoadFile(aPath.c_str());

	if(!lOK)
	{
		LOG("xml file load error %s", aPath.c_str());
		return;
	}

	TiXmlElement* lpConfigNode = lpXmlDoc->FirstChildElement();
	if(NULL == lpConfigNode || 0 != strcmp("configuration", lpConfigNode->Value()) )
	{
		LOG("config node not found");
		return;
	}

	lpConfigNode = lpConfigNode->FirstChildElement();
	if(NULL == lpConfigNode || 0 != strcmp("PC2App", lpConfigNode->Value()))
	{
		LOG("PC2App node not found");
		return;
	}

	apSettings->m_Type = lpConfigNode->Attribute("type");
	apSettings->m_NodeInfo.m_IP = lpConfigNode->Attribute("ip");
	apSettings->m_NodeInfo.m_Port = (unsigned short)atoi(lpConfigNode->Attribute("port"));

	if("client" != apSettings->m_Type)
	{
		apSettings->m_DBPath = lpConfigNode->Attribute("dbpath");
		apSettings->m_NodeInfo.m_Id = (uint32_t)atoi(lpConfigNode->Attribute("id"));
		apSettings->m_NodeInfo.m_Name = lpConfigNode->Attribute("name");
	}

	if("manager" == apSettings->m_Type)
	{
		TiXmlElement* lpElment = lpConfigNode->FirstChildElement();
		while(lpElment)
		{
			if(0 == strcmp("participant", lpElment->Value()))
			{
				NodeInfo lNode;
				lNode.m_Id = (uint32_t)atoi(lpElment->Attribute("id"));
				lNode.m_Port = (unsigned short)atoi(lpElment->Attribute("port"));
				lNode.m_Name = lpElment->Attribute("name");
				lNode.m_IP = lpElment->Attribute("ip");

				apSettings->m_Parts.push_back(lNode);
			}
			lpElment = lpElment->NextSiblingElement();
		}
	}
	else if("client" == apSettings->m_Type)
	{
		TiXmlElement* lpElment = lpConfigNode->FirstChildElement();
		while(lpElment)
		{
			if(0 == strcmp("manager", lpElment->Value()))
			{
				NodeInfo lNode;
				lNode.m_Port = (unsigned short)atoi(lpElment->Attribute("port"));
				lNode.m_IP = lpElment->Attribute("ip");

				apSettings->m_RemoteNodeInfo =lNode;
			}

			lpElment = lpElment->NextSiblingElement();
		}
	}
	else if("account" == apSettings->m_Type || "transaction" == apSettings->m_Type)
	{
		TiXmlElement* lpElment = lpConfigNode->FirstChildElement();
		while(lpElment)
		{
			if(0 == strcmp("manager", lpElment->Value()))
			{
				NodeInfo lNode;
				lNode.m_Id = (uint32_t)atoi(lpElment->Attribute("id"));
				lNode.m_Port = (unsigned short)atoi(lpElment->Attribute("port"));
				lNode.m_Name = lpElment->Attribute("name");
				lNode.m_IP = lpElment->Attribute("ip");

				apSettings->m_RemoteNodeInfo =lNode;
			}
			else if(0 == strcmp("participant", lpElment->Value()))
			{
				NodeInfo lNode;
				lNode.m_Id = (uint32_t)atoi(lpElment->Attribute("id"));
				lNode.m_Port = (unsigned short)atoi(lpElment->Attribute("port"));
				lNode.m_Name = lpElment->Attribute("name");
				lNode.m_IP = lpElment->Attribute("ip");

				apSettings->m_Parts.push_back(lNode);
			}
			lpElment = lpElment->NextSiblingElement();
		}
	}
	else
	{
		LOG("unknown app type %s", apSettings->m_Type.c_str());
	}

}





}
